<?php
/**
 * 设置服务类
 */
namespace app\index\service;

use Config;
use Cache;
use think\Db;

class Setting
{
    /**
     * 个人信息
     */
    public function info($member_id)
    {
        $member = Db::name('members')->where('member_id',$member_id)->field('nickname,username,phone,avatar,account,gold,age,sign_text,email,sex')->select()[0];
        $member['avatar'] = !empty($member['avatar']) ? 'yijiazhihui.40b876.cn/public'.$member['avatar'] : '';
        return $member;
    }

    /**
     * @param $member_id
     * @param $param
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 修改用户基本信息
     */
    public function editMemberInfo($member_id,$param)
    {
        $member = $this->info($member_id);
        if (!empty($param)) {
            foreach ($param as $key => $value) {
                if (!empty($value)) {
                    $res = Db::table('zh_members')->where('member_id', $member_id)->update(["$key" => $value]);
                }
            }
            return $member;
        }
    }

    /**
     * @param $member_id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取用户发票信息
     */
    public function getMemberInvoice($member_id)
    {
        $res = Db::table('zh_member_invoice')->where('member_id',$member_id)->select();
        return !empty($res) ? $res[0] : null;
    }

    /**
     * @param $member_id
     * @param $param
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * 修改发票
     */
    public function editMemberInvoice($member_id,$param)
    {
        if(!empty($param)){
            $memberInvoice = Db::table('zh_member_invoice')->where('member_id',$member_id)->select();
            if(!empty($memberInvoice)){
                Db::table('zh_member_invoice')->where('member_id',$member_id)->update($param);
            }else{
                $param['member_id'] = $member_id;
                Db::table('zh_member_invoice')->insert($param);
            }
        }
        return true;
    }


    /**
     * @param $member_id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取企业认证
     */
    public function getMemberBusinessCert($member_id)
    {
        $res = Db::table('zh_member_business_cert')->where('member_id',$member_id)->select();
        return $res ? $res[0] : null;
    }

    /**
     * @param $member_id
     * @param $param
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * 设置企业认证
     */
    public function setMemberBusinessCert($member_id,$param)
    {
        $res = $this->getMemberBusinessCert($member_id);
        if(!empty($res)){
            Db::table('zh_member_business_cert')->where('member_id',$member_id)->update($param);
        }else{
            Db::table('zh_member_business_cert')->insert($param);
        }
        return true;
    }

    /**
     * @param $member_id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取个人认证
     */
    public function getMemberPersonalCert($member_id)
    {
        $res = Db::table('zh_member_personal_cert')->where('member_id',$member_id)->select();
        return $res ? $res[0] : null;
    }

    /**
     * @param $member_id
     * @param $param
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * 设置个人认证
     */
    public function setMemberPersonalCert($member_id,$param)
    {
        $res = $this->getMemberPersonalCert($member_id);
        if(!empty($res)){
            Db::table('zh_member_personal_cert')->where('member_id',$member_id)->update($param);
        }else{
            Db::table('zh_member_personal_cert')->insert($param);
        }
        return true;
    }


    /**
     * @param $member_id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取工作信息
     */
    public function getMemberWorker($member_id)
    {
        $res = Db::table('zh_member_worker')->where('member_id',$member_id)->select();
        return $res ? $res[0] : null;
    }

    /**
     * @param $member_id
     * @param $param
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * 设置工作信息
     */
    public function setMemberWorker($member_id,$param)
    {
        $res = $this->getMemberWorker($member_id);
        if(!empty($res)){
            Db::table('zh_member_worker')->where('member_id',$member_id)->update($param);
        }else{
            Db::table('zh_member_worker')->insert($param);
        }
        return true;
    }


    /**
     * @param $member_id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取家庭信息
     */
    public function getFamily($member_id)
    {
        $res = Db::table('zh_member_family')->where('member_id',$member_id)->select();
        return $res ? $res[0] : null;
    }


    /**
     * @param $member_id
     * @param $param
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * 设置家庭信息
     */
    public function setFamily($member_id,$param)
    {
        $res = $this->getFamily($member_id);
        if(!empty($res)){

            Db::table('zh_member_family')->where('member_id',$member_id)->update($param);
        }else{
            $param['update_time'] = time();
            Db::table('zh_member_family')->insert($param);
        }
        return true;
    }

    /**
     * @param $member_id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 设置用户地址
     */
    public function getMemberAddress($member_id)
    {
        $res = Db::table('zh_member_address')->where('member_id',$member_id)->select();
        return $res ? $res[0] : null;
    }


    /**
     * @param $member_id
     * @param $param
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * 设置我的收货地址
     */
    public function setMemberAddress($member_id,$param)
    {
        $res = $this->getMemberAddress($member_id);
        if(!empty($res)){
            $param['createtime'] = time();
            Db::table('zh_member_address')->where('member_id',$member_id)->update($param);
        }else{
            $param['updatetime'] = time();
            Db::table('zh_member_address')->insert($param);
        }
        return true;
    }


}
